package com.siwei.infoextract.util;

import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

import com.siwei.infoextract.SystemConfig;
import com.siwei.infoextract.wrapper.parser.DomConverter;
import com.siwei.infoextract.wrapper.parser.DomNode;


public class Utility {

	public static String getContentFormURL(String url) throws IOException  {
		return getContentFormURL(url, null);
	}

	public static String getContentFormURL(String url, String encoding) throws IOException  {
		URL urlObj = new URL(url);  
		URLConnection conn = urlObj.openConnection();
		conn.connect();
	
		InputStream iStream = null;
		try {
			iStream = conn.getInputStream();
			return Utility.getContentFromInputStream(conn.getInputStream(), encoding);
		} finally {
			if (iStream != null) {
				iStream.close();
			}
		}
	}
	
	public static String getContentFromFile(String file) throws IOException {
		return getContentFromFile(file, null);
	}

	public static String getContentFromFile(String file, String encoding) throws IOException {
		FileInputStream fiStream = null;
		try {
			fiStream = new FileInputStream(file);
			return Utility.getContentFromInputStream(fiStream, encoding);
		} finally {
			if (fiStream != null) {
				fiStream.close();
			}
		}
	}

	public static String getContentFromInputStream(InputStream inputStream, String encoding) throws IOException {
		StringBuffer htmlBuffer = new StringBuffer();
		InputStreamReader isReader = null;
		try {
			if (encoding != null) {
				isReader = new InputStreamReader(inputStream, encoding);
			} else {
				isReader = new InputStreamReader(inputStream, SystemConfig.getValue(SystemConfig.DEFAULT_ENCODING_KEY));
			}

			char[] charsRead = new char[256];
			int charsReadCount = 0;
			while((charsReadCount = isReader.read(charsRead)) > 0) {
				htmlBuffer.append(charsRead, 0, charsReadCount);
			}

			return htmlBuffer.toString();
		} finally {
			if (isReader != null) {
				isReader.close();
			}
		}
	}

	public static void saveContentToFile(String content, String file) throws IOException {
		FileWriter fileWriter = null;
		try {
			fileWriter = new FileWriter(file, false);
			fileWriter.write(content);
		} finally  {
			if (fileWriter != null) {
				fileWriter.close();
			}
		}
	}

	public static DomNode parseDomNodeFromURL(String url) throws IOException {
		URL urlObj = new URL(url);  
		URLConnection conn = urlObj.openConnection();
		conn.connect();

		InputStream iStream = conn.getInputStream();
		try {
			DomConverter converter = new DomConverter(iStream);
		return converter.GenerateDomTree();
		} finally {
			if (iStream != null) {
				iStream.close();
			}
		}
	}

	public static DomNode parseDomNodeFromFile(String file) throws IOException {
		InputStream iStream = null;
		try {
			iStream = new FileInputStream(file);
			DomConverter converter = new DomConverter(iStream);
			return converter.GenerateDomTree();
		} finally {
			if (iStream != null) {
				iStream.close();
			}
		}
	}

}
